{extend name="public/layout" /}
{block name="body"}
    <form class="layui-form" > <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
        <div class="layui-card pd-bt-15">
            <div class="layui-row">
                <div class="layui-row">
                    <div class="layui-col-md12">
                        <div class="layui-form-item">
                            <label class="layui-form-label single-upload-label">类目图</label>
                            <div class="layui-upload" style="display: inline-block">
                                <button type="button" class="layui-btn" id="upload-normal" style="vertical-align: bottom">上传图片</button>
                                <input class="layui-upload-file" type="file" accept="undefined" name="pic">
                                    <img {if condition="isset($vo) && $vo.logo"}
                                        src="{$vo.logo}"
                                    {/if} class="layui-upload-img" id="test-upload-normal-img">
                                <input type="hidden" id="logo" name="logo" value="{$vo.logo|default=""}"/>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-row">
                <div class="layui-col-md8">
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类名</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" value="{$vo.name|default=''}" lay-verify="required"  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md4">
                    <div class="layui-form-mid layui-word-aux">请输入分类名</div>
                </div>
            </div>
            <div class="layui-row">
                <div class="layui-col-md8">
                    <div class="layui-form-item">
                        <label class="layui-form-label">排序</label>
                        <div class="layui-input-block">
                            <input type="text" name="sort" value="{$vo.sort|default=999}" lay-verify="required"  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md4">
                    <div class="layui-form-mid layui-word-aux">排序，越少越前</div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">显示在首页</label>
                <div class="layui-input-block">
                    <input name="is_index" type="checkbox" lay-skin="switch" {if condition="isset($vo) && $vo.is_index eq 1"}checked{/if}>
                </div>
            </div>
            {if condition="isset($is_update) && $is_update eq 1"}
                <input type="hidden" name="id" value="{$vo.id}"/>
            {/if}
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
                </div>
            </div>
        </div>
    </form>
{/block}
{block name="script"}
    <script>
        layui.use(['form','upload'],function(){
            var form = layui.form,
                $ = layui.$;
            form.on('submit(*)', function(data){
                {if condition="isset($is_update) && $is_update eq 1"}
                var url =  "{:url('update')}";
                {else/}
                var url =  "{:url('add')}";
                {/if}
                $.post(url,data.field,function(data){
                    if(data.code == 10000)
                    {
                        layer.msg(data.msg,{icon:1,time:1500},function(){
                            window.location.href = "{:url('index')}";
                        })
                    }else{
                        layer.msg(data.msg,{icon:2,time:1500})
                    }

                });
                return false;
            });
            var upload = layui.upload;

            var uploadInst = upload.render({
                elem: '#upload-normal'
                ,url: "{:url('util/upload')}"
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#test-upload-normal-img').attr('src', result); //图片链接（base64）
                    });
                }
                ,done: function(res){
                    //如果上传失败
                    if(res.code != 10000){
                        return layer.msg(res.msg);
                    }else{
                        $('#logo').val(res.data.url);
                    }
                    //上传成功
                }
                ,error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#test-upload-demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                }
            });
        });
    </script>
{/block}